using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class Health_Manager : MonoBehaviour {
	
	public int maxHealth;
	public int health;
	public float HPRegen = 0f;
	
	public int maxEnergy;
	public int energy;
	public float ENRegen = 0f;

	public bool invincible = false;
	public bool dead  = false;

	public Transform damageEffectTransform ;
	public Dictionary<Spell_Power_Type,float> Resistance;	
	
	
	private float lastDamageTime = 0f;
	private GameObject damageEffect ;
	private int healthBarLenght = 20;
	private Vector3 screenPosition;
	void Awake()
	{
		Resistance = new Dictionary<Spell_Power_Type, float>();
	}
	
	public void setWorldParams(NPC_Stats stats)
	{
		Resistance.Clear();
		int p = 0;
		foreach( Spell_Power_Type r in stats.Resistances_Types ){
			Resistance.Add(r,stats.Resistance_Values[p]);
			p++;
		}
		
		maxHealth = stats.Life_Cap;
		maxEnergy = stats.Energy_Cap;
	}
	
	public void onDmg(Spell_Power_Type type, int dmg, GameObject fx = null)
	{
		if(invincible)
			return;
		if (dead)
			return;
		if (dmg <= 0)
			return;
		float res=0f;
		if(Resistance.ContainsKey(type))
		{
			res=(float)(((float)Resistance[type])*0.1f)*dmg;
			if(res>=dmg){
				res=0; dmg=1;
			}
		}
		
		health -= (int)(dmg-res);
		lastDamageTime = Time.time;
		if(fx!=null && damageEffect == null)
		{	
			damageEffect = (GameObject)Instantiate(fx,transform.position,transform.rotation);
			ParticleSystem ss = (ParticleSystem)damageEffect.GetComponent(typeof(ParticleSystem));
			ss.enableEmission = true;
			Destroy(damageEffect,ss.duration);
			
		}
	}
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		DrawBar();
	}
	void DrawBar()
	{
  		screenPosition = Camera.main.WorldToScreenPoint(transform.position);
     	screenPosition.y = Screen.height - screenPosition.y;
 		
    	GUI.Box(new Rect(screenPosition.x-10,screenPosition.y-40,healthBarLenght,20),health+"/"+maxHealth);
		GUI.Box(new Rect(screenPosition.x-9,screenPosition.y-41,(health / maxHealth),20),"");
	}
	
}
